//
//  ZYStatusOriginalView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/13.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit
import SnapKit

class ZYStatusOriginalView: UIView {
    
    var originalViewBottomConstraint: Constraint?
    
    var statusViewModel:ZYStatusViewModel?{
        didSet{
//            print("============",contentLabel.text)
            headImageView.zy_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            timeLabel.text = statusViewModel?.sinaTimeStr//微博时间
            sourceLabel.text = statusViewModel?.sinaSourceStr
            avatarImageView.image = statusViewModel?.verifiedImage
            contentLabel.text = statusViewModel?.statusModel?.text
            membershipImageView.image = statusViewModel?.mbrankImage
            
            //卸载约束
            originalViewBottomConstraint?.uninstall()
            if statusViewModel?.statusModel?.pic_urls?.count > 0 {
                //赋值
                pictureView.pictureInfos = statusViewModel?.statusModel?.pic_urls
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).constraint
                })
                //显示
                pictureView.hidden = false
            }else{
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).constraint
                })
                //隐藏
                pictureView.hidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //约束
        //1
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }//3
        membershipImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        //5
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //7
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(16)
            make.leading.equalTo(10)
        }
        //8
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
//            make.size.equalTo(CGSize(width: 100, height: 100))//内部约束了
//            make.leading.equalTo(10)
            make.leading.equalTo((SCREENW - 250)/2)
        }
        //自身约束
        self.snp_makeConstraints { (make) -> Void in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        //测试数据
//        nameLabel.text = "name"
//        timeLabel.text = "time"
//        sourceLabel.text = "source"
//        contentLabel.text = "1111hsjadjakfdjkfjadjlfakjdfakdfjadshsjadjakfdjkfjadjlfakjdfakdfjadshsjadjakfdjkfjadjlfakjdfakdfjadshsjadjakfdjkfjadjlfakjdfakdfjadshsjadjakfdjkfjadjlfakjdfakdfjadshsjadjakfdjkfjadjlfakjdfakdfjads2222"
    }
    // MARK: - 懒加载 控件
    //1 头像
    private lazy var headImageView:UIImageView = UIImageView(imgName: "avatar_default")
    //2 名字
    private lazy var nameLabel:UILabel = {
       let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        return lab
    }()
    //3 用户等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //4 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: AppThemeColor, textSize:10)
    //5 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textSize: 10)
    //6 认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //7 微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: SCREENW - 20)
    
    //8 配图
    private lazy var pictureView:ZYStatusPictureView = ZYStatusPictureView()
    
}
